package com.selvn.encryptnote;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

public class Func
{
	/**
	 * @param args
	 */
	public static String date_YMD( Date date )
	{
		return new SimpleDateFormat("yyyy-MM-dd").format(date);
	}

	public static String date_YMD( String dateString )
	{
		if (dateString.indexOf("-") >= 0)
		{
			return dateString;
		}
		Date date = date_parse(dateString);
		return date_YMD(date);
	}

	public static Date date_parse( String dateString )
	{
		Date date = null;
		try
		{
			if (dateString.indexOf("-") >= 0)
			{
				date = new SimpleDateFormat("yyyy-MM-dd").parse(dateString);
			} else
			{
				date = new Date(Long.parseLong(dateString));
			}
		} catch (Exception e)
		{
			Log.e(Config.LOGTAG, "Error when parsing date string: " + dateString);
			date = new Date();
		}
		return date;
	}

	public static boolean CheckFileExist( String filePath )
	{
		return new File(filePath).exists();
	}

	public static boolean DeleteFile( String filePath )
	{
		File file;
		file = new File(filePath);
		if (file.exists())
		{
			return file.delete();
		} else
		{
			return false;
		}
	}

	public static boolean WriteFile( String filePath, String fileString )
	{
		try
		{
			File file = new File(filePath);
			file.mkdirs();
			// delete the file if exist
			DeleteFile(filePath);
			FileOutputStream outStream = new FileOutputStream(file);
			outStream.write(fileString.getBytes());
			outStream.close();
		} catch (IOException e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static String ReadFile( String filePath )
	{
		String content = "";
		if (!new File(filePath).exists())
		{
			return content;
		}
		BufferedReader reader = null;
		try
		{
			reader = new BufferedReader(new FileReader(filePath));

			String line;
			while ((line = reader.readLine()) != null)
			{
				content += line;
			}
		} catch (IOException e)
		{
			e.printStackTrace();
		} finally
		{
			if (reader != null)
			{
				try
				{
					reader.close();
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
		return content;
	}

	public static int WriteFileToInternal( Context context, String filePath, String fileString )
	{
		FileOutputStream fos = null;
		try
		{
			fos = context.openFileOutput(filePath, Context.MODE_PRIVATE);
			fos.write(fileString.getBytes());
			fos.close();
		} catch (Exception e)
		{
			e.printStackTrace();
			return Config.WRITE_INTERNAL_FILE_FAILED;
		}
		return Config.WRITE_INTERNAL_FILE_SUCCEED;
	}

	public static String ReadFileFromInternal( Context context, String filePath )
	{
		FileInputStream fis;
		StringBuffer fileContent = new StringBuffer("");
		try
		{
			fis = context.openFileInput(filePath);
			byte[] buffer = new byte[1024];
			while ((fis.read(buffer)) != -1)
			{
				fileContent.append(new String(buffer));
			}
			fis.close();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return fileContent.toString();
	}

	public static boolean isExternalStorageAvailable()
	{
		boolean mExternalStorageAvailable = false;
		boolean mExternalStorageWriteable = false;
		String state = Environment.getExternalStorageState();

		if (Environment.MEDIA_MOUNTED.equals(state))
		{
			// We can read and write the media
			mExternalStorageAvailable = mExternalStorageWriteable = true;
		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
		{
			// We can only read the media
			mExternalStorageAvailable = true;
			mExternalStorageWriteable = false;
		} else
		{
			// Something else is wrong. It may be one of many other states, but
			// all we need
			// to know is we can neither read nor write
			mExternalStorageAvailable = mExternalStorageWriteable = false;
		}
		return mExternalStorageAvailable && mExternalStorageWriteable;
	}

	public static void MessageBox( Context c, int message, int title, int PositiveText )
	{
		new AlertDialog.Builder(c).setTitle(title).setMessage(message).setPositiveButton(PositiveText, new DialogInterface.OnClickListener()
		{
			public void onClick( DialogInterface dialog, int which )
			{
				// finish();
			}
		})
		/*
		 * .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
		 * 
		 * public void onClick(DialogInterface dialog, int which) { finish(); }
		 * }) .setNeutralButton("Neutral",new DialogInterface.OnClickListener()
		 * {
		 * 
		 * public void onClick(DialogInterface dialog, int which) { finish(); }
		 * })
		 */
		.show();
	}

	public static int handle_return_code( Context context, int code )
	{
		switch (code)
		{
			case Config.SAVE_SUCCEED:
				Toast.makeText(context.getApplicationContext(), R.string.save_succeed, Toast.LENGTH_SHORT);
				break;
			case Config.SAVE_FAILED_BLANK:
				Toast.makeText(context, R.string.save_failed_blank, Toast.LENGTH_SHORT);
				break;
			case Config.BACKUP_SUCCEED:
				Toast.makeText(context, R.string.backupSuccessfulMessage, Toast.LENGTH_LONG).show();
				break;
			case Config.BACKUP_FAILED:
				Toast.makeText(context, R.string.backupFailMessage, Toast.LENGTH_LONG).show();
				break;
			case Config.BACKUP_FAILED_ENCRYPT_ERROR:
				Toast.makeText(context, "备份失败", Toast.LENGTH_LONG).show();
				break;
			case Config.BACKUP_FAILED_NO_EXTERNAL:
				Toast.makeText(context, R.string.BACKUP_ExternalStorageError, Toast.LENGTH_LONG).show();
				break;
			case Config.RESTORE_SUCCEED:
				Toast.makeText(context, R.string.restore_succeed, Toast.LENGTH_LONG).show();
				break;
			case Config.RESTORE_FAILED_NOFILEFOUND:
				Toast.makeText(context, R.string.noRestoreFileFound, Toast.LENGTH_LONG).show();
				break;
			case Config.RESTORE_PARSE_FAILED:
				Toast.makeText(context, "备份文件损坏", Toast.LENGTH_LONG).show();
				break;
			default:
				break;
		}
		return code;
	}
}
